﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace 医院药库管理系统
{
    public partial class 药品入库单 : Form
    {
        public String Name1;
        public 药品入库单()
        {
            InitializeComponent();
        }

        private void bt_rkfh_Click(object sender, EventArgs e)
        {
            首页 f3 = new 首页();
            f3.Show();
        }

        private void tb_rkbh_TextChanged(object sender, EventArgs e)
        {

        }

        private void bt_rktj_Click(object sender, EventArgs e)
        {

            if (tb_rkbh.Text == "" || tb_rkmc.Text == "" || tb_rksl.Text == "" || tb_rkszm.Text == "" || dtp_scrq.Text == "" || dtp_yxq.Text == "" || tb_rkjj.Text == "" || tb_rkcj.Text == "" || tb_rkls.Text == "" || cb_yplx.Text == "" || cb_wlzt.Text == "" || cb_rkjx.Text == "")
            {
                MessageBox.Show("信息不能为空！");
                return;
            }
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText =
                "INSERT tb_Medicine (No,Name,Acronym,Made_Date,Valid_Date,InDate,FirmName,PurchasePrice,Price,Quantity,Type,PhysicalState,DosageForm,in_principal) VALUES(@No,@Name,@Acronym,@Made_Date,@Valid_Date,@InDate,@FirmName,@PurchasePrice,@Price,@Quantity, @Type, @PhysicalState,@DosageForm,@in_principal);";
            sqlCommand.Parameters.AddWithValue("@No" ,this.tb_rkbh.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Name", this.tb_rkmc.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Acronym", this.tb_rkszm.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Made_Date", this.dtp_scrq.Value);
            sqlCommand.Parameters.AddWithValue("@Valid_Date", this.dtp_yxq.Value);
            sqlCommand.Parameters.AddWithValue("@InDate",DateTime.Now);
            sqlCommand.Parameters.AddWithValue("@FirmName", this.tb_rkcj.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@PurchasePrice", this.tb_rkjj.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Price", this.tb_rkls.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Quantity", this.tb_rksl.Text.Trim());



            sqlCommand.Parameters.AddWithValue("@Type", this.cb_yplx.SelectedIndex+1);



            sqlCommand.Parameters.AddWithValue("@PhysicalState", this.cb_wlzt.SelectedIndex+1);

           

            sqlCommand.Parameters.AddWithValue("@DosageForm", this.cb_rkjx.SelectedIndex+1);
            sqlCommand.Parameters.AddWithValue("@in_principal", Name1);
            
          
            SqlCommand sqlCommand3 = sqlConnection.CreateCommand();
            sqlCommand3.CommandText = "select count(*) from tb_Firm where Name=@FirmName;";
            sqlCommand3.Parameters.AddWithValue("@FirmName", this.tb_rkcj.Text.Trim());
            SqlCommand sqlCommand4 = sqlConnection.CreateCommand();
            sqlCommand4.CommandText = "select count(*) from tb_Firm ;";

            sqlConnection.Open();
            object obj = sqlCommand3.ExecuteScalar();
            object obj1 = sqlCommand4.ExecuteScalar();
            sqlConnection.Close();
            int count = (int)obj;
            int n = (int)obj1;
            SqlCommand sqlCommand2 = sqlConnection.CreateCommand();
            
            if (count == 0)
            {

                sqlCommand2.CommandText =
                   "INSERT tb_Firm (No,Name,Attribution)VALUES(" + (n + 1) + ",@Name11,@Attribution);";
                sqlCommand2.Parameters.AddWithValue("@Name11", this.tb_rkcj.Text.Trim());
                sqlCommand2.Parameters.AddWithValue("@Attribution", this.tb_gsd.Text.Trim());
                sqlConnection.Open();
                
                int rowAffected2 = sqlCommand2.ExecuteNonQuery();
               
                sqlConnection.Close();
                if (rowAffected2 != 0)
                {
                    MessageBox.Show("厂商有新记录增加!");

                }
            }

            SqlCommand sqlCommand5 = sqlConnection.CreateCommand();

             if (Convert.ToInt32(this.tb_rksl.Text.ToString()) < Convert.ToInt32(this.dgv_sq.Rows[this.dgv_sq.CurrentRow.Index].Cells[4].Value.ToString()))
            {
                sqlCommand5.CommandText = "UPDATE tb_Report SET Quantity-=@Quantity WHERE Name=@Name5 And No=@No5;";
                sqlCommand5.Parameters.AddWithValue("@Name5", this.tb_rkmc.Text.Trim());
                sqlCommand5.Parameters.AddWithValue("@No5", Convert.ToInt32(this.dgv_sq.Rows[this.dgv_sq.CurrentRow.Index].Cells[0].Value.ToString()));
                sqlCommand5.Parameters.AddWithValue("@Quantity", Convert.ToInt32(this.tb_rksl.Text.ToString()));
            }

             else 
             {
                 sqlCommand5.CommandText = "UPDATE tb_Report SET zt='已处理' WHERE Name=@Name5 And No=@No5;";
                 sqlCommand5.Parameters.AddWithValue("@Name5", this.tb_rkmc.Text.Trim());
                 sqlCommand5.Parameters.AddWithValue("@No5", Convert.ToInt32(this.dgv_sq.Rows[this.dgv_sq.CurrentRow.Index].Cells[0].Value.ToString()));
             }

            sqlConnection.Open();
            int rowAffected = sqlCommand.ExecuteNonQuery();
            
            int rowAffected3 = sqlCommand5.ExecuteNonQuery();
            sqlConnection.Close();

            if (rowAffected != 0)
            {
                MessageBox.Show("入库成功。");
               
            }
            else
            {
                MessageBox.Show("入库失败！");
            }
            if (rowAffected3== 1)
            {
                MessageBox.Show("采购申请处理成功！");
               

            }
           
           
        }

        private void 药品入库单_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“yyaoDataSet1.tb_Physicalstate”中。您可以根据需要移动或移除它。
            this.tb_PhysicalstateTableAdapter.Fill(this.yyaoDataSet1.tb_Physicalstate);
            // TODO: 这行代码将数据加载到表“yyaoDataSet1.tb_Type”中。您可以根据需要移动或移除它。
            this.tb_TypeTableAdapter.Fill(this.yyaoDataSet1.tb_Type);
            // TODO: 这行代码将数据加载到表“yyaoDataSet1.tb_DosageForm”中。您可以根据需要移动或移除它。
            this.tb_DosageFormTableAdapter.Fill(this.yyaoDataSet1.tb_DosageForm);

            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT * FROM tb_Report;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataTable csTable = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter.Fill(csTable);
            sqlConnection.Close();
            this.dgv_sq.DataSource = csTable;
            this.dgv_sq.Columns["No"].HeaderText = "编号";
            this.dgv_sq.Columns["Name"].HeaderText = "名称";
            this.dgv_sq.Columns["ReportDate"].HeaderText = "申请日期";
            this.dgv_sq.Columns["FirmName"].HeaderText = "厂商名称";
            this.dgv_sq.Columns["DosageForm"].HeaderText = "剂型";
            this.dgv_sq.Columns["Principal"].HeaderText = "负责人";
            this.dgv_sq.Columns["Quantity"].HeaderText = "数量";
            this.dgv_sq.Columns["zt"].HeaderText = "申请状态";
            

        }

        private void gb_ypxx_Enter(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void lb_zr_Click(object sender, EventArgs e)
        {
            this.tb_rkmc.Text = this.dgv_sq.Rows[this.dgv_sq.CurrentRow.Index].Cells[1].Value.ToString();
            this.tb_rkcj.Text = this.dgv_sq.Rows[this.dgv_sq.CurrentRow.Index].Cells[3].Value.ToString();
            this.tb_rksl.Text = this.dgv_sq.Rows[this.dgv_sq.CurrentRow.Index].Cells[4].Value.ToString();
            this.cb_rkjx.Text = this.dgv_sq.Rows[this.dgv_sq.CurrentRow.Index].Cells[5].Value.ToString();
        }

        private void tb_rksl_TextChanged(object sender, EventArgs e)
        {
            
        }
    }
}
